<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>状态保持 | 欢迎学习django课程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../part3/7.html" />
    
    
    <link rel="prev" href="../part3/5.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="3.5"
        data-chapter-title="状态保持"
        data-filepath="part3/6.md"
        data-basepath=".."
        data-revision="Tue Jan 03 2017 08:43:08 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="part1/1.html">
            
                
                    <a href="../part1/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="part1/2.html">
            
                
                    <a href="../part1/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="part1/3.html">
            
                
                    <a href="../part1/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        设计模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="part1/4.html">
            
                
                    <a href="../part1/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        管理站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="part1/5.html">
            
                
                    <a href="../part1/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="part1/6.html">
            
                
                    <a href="../part1/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="part1/7.html">
            
                
                    <a href="../part1/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="part2/1.html">
            
                
                    <a href="../part2/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模型
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="part2/2.html">
            
                
                    <a href="../part2/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="part2/3.html">
            
                
                    <a href="../part2/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模型成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="part2/4.html">
            
                
                    <a href="../part2/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        模型查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="part2/5.html">
            
                
                    <a href="../part2/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="part2/6.html">
            
                
                    <a href="../part2/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="part3/1.html">
            
                
                    <a href="../part3/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="part3/2.html">
            
                
                    <a href="../part3/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        URLconf
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="part3/3.html">
            
                
                    <a href="../part3/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="part3/4.html">
            
                
                    <a href="../part3/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Request对象
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="part3/4_1.html">
            
                
                    <a href="../part3/4_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        QueryDict对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="part3/4_2.html">
            
                
                    <a href="../part3/4_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GET属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="part3/4_3.html">
            
                
                    <a href="../part3/4_3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        POST属性
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="part3/5.html">
            
                
                    <a href="../part3/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Response对象
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="3.5" data-path="part3/6.html">
            
                
                    <a href="../part3/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        状态保持
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="part3/7.html">
            
                
                    <a href="../part3/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="part4/1.html">
            
                
                    <a href="../part4/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="part4/2.html">
            
                
                    <a href="../part4/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        定义模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="part4/3.html">
            
                
                    <a href="../part4/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        模板继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="part4/4.html">
            
                
                    <a href="../part4/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        HTML转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="part4/5.html">
            
                
                    <a href="../part4/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="part4/6.html">
            
                
                    <a href="../part4/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="part4/7.html">
            
                
                    <a href="../part4/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="part5/1.html">
            
                
                    <a href="../part5/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="part5/2.html">
            
                
                    <a href="../part5/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        静态文件处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="part5/3.html">
            
                
                    <a href="../part5/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="part5/4.html">
            
                
                    <a href="../part5/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="part5/5.html">
            
                
                    <a href="../part5/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="part5/6.html">
            
                
                    <a href="../part5/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="part5/7.html">
            
                
                    <a href="../part5/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        ajax
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="part5/8.html">
            
                
                    <a href="../part5/8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="part6/1.html">
            
                
                    <a href="../part6/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第三方
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="part6/2.html">
            
                
                    <a href="../part6/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="part6/3.html">
            
                
                    <a href="../part6/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.3" data-path="part6/4.html">
            
                
                    <a href="../part6/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        全文检索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="part6/5.html">
            
                
                    <a href="../part6/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        celery
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="part6/6.html">
            
                
                    <a href="../part6/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        布署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="part6/7.html">
            
                
                    <a href="../part6/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="part7/1.html">
            
                
                    <a href="../part7/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Git与项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="part7/2.html">
            
                
                    <a href="../part7/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        Git简介
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part7/2_2.html">
            
                
                    <a href="../part7/2_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.1.</b>
                        
                        远程仓库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part7/2_1.html">
            
                
                    <a href="../part7/2_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.2.</b>
                        
                        本地仓库
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="part7/3.html">
            
                
                    <a href="../part7/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        项目设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="part7/4.html">
            
                
                    <a href="../part7/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        模型类设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="part7/5.html">
            
                
                    <a href="../part7/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        使用页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="part7/6.html">
            
                
                    <a href="../part7/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >欢迎学习django课程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x72B6;&#x6001;&#x4FDD;&#x6301;">&#x72B6;&#x6001;&#x4FDD;&#x6301;</h1>
<ul>
<li>http&#x534F;&#x8BAE;&#x662F;&#x65E0;&#x72B6;&#x6001;&#x7684;&#xFF1A;&#x6BCF;&#x6B21;&#x8BF7;&#x6C42;&#x90FD;&#x662F;&#x4E00;&#x6B21;&#x65B0;&#x7684;&#x8BF7;&#x6C42;&#xFF0C;&#x4E0D;&#x4F1A;&#x8BB0;&#x5F97;&#x4E4B;&#x524D;&#x901A;&#x4FE1;&#x7684;&#x72B6;&#x6001;</li>
<li>&#x5BA2;&#x6237;&#x7AEF;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x7684;&#x4E00;&#x6B21;&#x901A;&#x4FE1;&#xFF0C;&#x5C31;&#x662F;&#x4E00;&#x6B21;&#x4F1A;&#x8BDD;</li>
<li>&#x5B9E;&#x73B0;&#x72B6;&#x6001;&#x4FDD;&#x6301;&#x7684;&#x65B9;&#x5F0F;&#xFF1A;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#x6216;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5B58;&#x50A8;&#x4E0E;&#x4F1A;&#x8BDD;&#x6709;&#x5173;&#x7684;&#x6570;&#x636E;</li>
<li>&#x5B58;&#x50A8;&#x65B9;&#x5F0F;&#x5305;&#x62EC;cookie&#x3001;session&#xFF0C;&#x4F1A;&#x8BDD;&#x4E00;&#x822C;&#x6307;session&#x5BF9;&#x8C61;</li>
<li>&#x4F7F;&#x7528;cookie&#xFF0C;&#x6240;&#x6709;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x5728;&#x5BA2;&#x6237;&#x7AEF;&#xFF0C;&#x6CE8;&#x610F;&#x4E0D;&#x8981;&#x5B58;&#x50A8;&#x654F;&#x611F;&#x4FE1;&#x606F;</li>
<li>&#x63A8;&#x8350;&#x4F7F;&#x7528;sesison&#x65B9;&#x5F0F;&#xFF0C;&#x6240;&#x6709;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#xFF0C;&#x5728;&#x5BA2;&#x6237;&#x7AEF;cookie&#x4E2D;&#x5B58;&#x50A8;session_id</li>
<li>&#x72B6;&#x6001;&#x4FDD;&#x6301;&#x7684;&#x76EE;&#x7684;&#x662F;&#x5728;&#x4E00;&#x6BB5;&#x65F6;&#x95F4;&#x5185;&#x8DDF;&#x8E2A;&#x8BF7;&#x6C42;&#x8005;&#x7684;&#x72B6;&#x6001;&#xFF0C;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x8DE8;&#x9875;&#x9762;&#x8BBF;&#x95EE;&#x5F53;&#x524D;&#x8BF7;&#x6C42;&#x8005;&#x7684;&#x6570;&#x636E;</li>
<li>&#x6CE8;&#x610F;&#xFF1A;&#x4E0D;&#x540C;&#x7684;&#x8BF7;&#x6C42;&#x8005;&#x4E4B;&#x95F4;&#x4E0D;&#x4F1A;&#x5171;&#x4EAB;&#x8FD9;&#x4E2A;&#x6570;&#x636E;&#xFF0C;&#x4E0E;&#x8BF7;&#x6C42;&#x8005;&#x4E00;&#x4E00;&#x5BF9;&#x5E94;</li>
</ul>
<h4 id="&#x542F;&#x7528;session">&#x542F;&#x7528;session</h4>
<ul>
<li>&#x4F7F;&#x7528;django-admin startproject&#x521B;&#x5EFA;&#x7684;&#x9879;&#x76EE;&#x9ED8;&#x8BA4;&#x542F;&#x7528;</li>
<li>&#x5728;settings.py&#x6587;&#x4EF6;&#x4E2D;</li>
</ul>
<pre><code>&#x9879;INSTALLED_APPS&#x5217;&#x8868;&#x4E2D;&#x6DFB;&#x52A0;&#xFF1A;
&apos;django.contrib.sessions&apos;,

&#x9879;MIDDLEWARE_CLASSES&#x5217;&#x8868;&#x4E2D;&#x6DFB;&#x52A0;&#xFF1A;
&apos;django.contrib.sessions.middleware.SessionMiddleware&apos;,
</code></pre><ul>
<li>&#x7981;&#x7528;&#x4F1A;&#x8BDD;&#xFF1A;&#x5220;&#x9664;&#x4E0A;&#x9762;&#x6307;&#x5B9A;&#x7684;&#x4E24;&#x4E2A;&#x503C;&#xFF0C;&#x7981;&#x7528;&#x4F1A;&#x8BDD;&#x5C06;&#x8282;&#x7701;&#x4E00;&#x4E9B;&#x6027;&#x80FD;&#x6D88;&#x8017;</li>
</ul>
<h4 id="&#x4F7F;&#x7528;session">&#x4F7F;&#x7528;session</h4>
<ul>
<li>&#x542F;&#x7528;&#x4F1A;&#x8BDD;&#x540E;&#xFF0C;&#x6BCF;&#x4E2A;HttpRequest&#x5BF9;&#x8C61;&#x5C06;&#x5177;&#x6709;&#x4E00;&#x4E2A;session&#x5C5E;&#x6027;&#xFF0C;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x7C7B;&#x5B57;&#x5178;&#x5BF9;&#x8C61;</li>
<li>get(key, default=None)&#xFF1A;&#x6839;&#x636E;&#x952E;&#x83B7;&#x53D6;&#x4F1A;&#x8BDD;&#x7684;&#x503C;</li>
<li>clear()&#xFF1A;&#x6E05;&#x9664;&#x6240;&#x6709;&#x4F1A;&#x8BDD;</li>
<li>flush()&#xFF1A;&#x5220;&#x9664;&#x5F53;&#x524D;&#x7684;&#x4F1A;&#x8BDD;&#x6570;&#x636E;&#x5E76;&#x5220;&#x9664;&#x4F1A;&#x8BDD;&#x7684;Cookie</li>
<li>del request.session[&apos;member_id&apos;]&#xFF1A;&#x5220;&#x9664;&#x4F1A;&#x8BDD;</li>
</ul>
<h4 id="&#x7528;&#x6237;&#x767B;&#x5F55;&#x793A;&#x4F8B;">&#x7528;&#x6237;&#x767B;&#x5F55;&#x793A;&#x4F8B;</h4>
<ul>
<li>&#x64CD;&#x4F5C;&#x6548;&#x679C;&#x5982;&#x4E0B;&#x56FE;&#xFF1A;</li>
</ul>
<p><img src="images/login.gif" alt="&#x767B;&#x5F55;&#x6F14;&#x793A;"></p>
<ul>
<li>&#x5728;views.py&#x6587;&#x4EF6;&#x4E2D;&#x521B;&#x5EFA;&#x89C6;&#x56FE;</li>
</ul>
<pre><code>from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse

def index(request):
    uname = request.session.get(&apos;uname&apos;)
    return render(request, &apos;booktest/index.html&apos;, {&apos;uname&apos;: uname})

def login(request):
    return render(request, &apos;booktest/login.html&apos;)

def login_handle(request):
    request.session[&apos;uname&apos;] = request.POST[&apos;uname&apos;]
    return redirect(reverse(&apos;main:index&apos;))

def logout(request):
    # request.session[&apos;uname&apos;] = None
    # del request.session[&apos;uname&apos;]
    # request.session.clear()
    request.session.flush()
    return redirect(reverse(&apos;main:index&apos;))
</code></pre><ul>
<li>&#x914D;&#x7F6E;url</li>
</ul>
<pre><code>&#x4E3B;url&#xFF1A;
from django.conf.urls import include, url
urlpatterns = [
    url(r&apos;^&apos;, include(&apos;booktest.urls&apos;, namespace=&apos;main&apos;))
]

&#x5E94;&#x7528;url&#xFF1A;
from django.conf.urls import url
from . import views
urlpatterns = [
    url(r&apos;^$&apos;, views.index, name=&apos;index&apos;),
    url(r&apos;login/$&apos;, views.login, name=&apos;login&apos;),
    url(r&apos;login_handle/$&apos;, views.login_handle, name=&apos;login_handle&apos;),
    url(r&apos;logout/$&apos;, views.logout, name=&apos;logout&apos;)
]
</code></pre><ul>
<li>&#x521B;&#x5EFA;&#x6A21;&#x677F;index.html</li>
</ul>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&#x9996;&#x9875;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&#x4F60;&#x597D;&#xFF1A;{{uname}}
&lt;hr/&gt;
&lt;a href=&quot;{%url &apos;main:login&apos;%}&quot;&gt;&#x767B;&#x5F55;&lt;/a&gt;
&lt;hr/&gt;
&lt;a href=&quot;{%url &apos;main:logout&apos;%}&quot;&gt;&#x9000;&#x51FA;&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre><ul>
<li>&#x521B;&#x5EFA;&#x6A21;&#x677F;login.html</li>
</ul>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&#x767B;&#x5F55;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form method=&quot;post&quot; action=&quot;/login_handle/&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;uname&quot;/&gt;
    &lt;input type=&quot;submit&quot; value=&quot;&#x767B;&#x5F55;&quot;/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre><h4 id="&#x4F1A;&#x8BDD;&#x8FC7;&#x671F;&#x65F6;&#x95F4;">&#x4F1A;&#x8BDD;&#x8FC7;&#x671F;&#x65F6;&#x95F4;</h4>
<ul>
<li>set_expiry(value)&#xFF1A;&#x8BBE;&#x7F6E;&#x4F1A;&#x8BDD;&#x7684;&#x8D85;&#x65F6;&#x65F6;&#x95F4;</li>
<li>&#x5982;&#x679C;&#x6CA1;&#x6709;&#x6307;&#x5B9A;&#xFF0C;&#x5219;&#x4E24;&#x4E2A;&#x661F;&#x671F;&#x540E;&#x8FC7;&#x671F;</li>
<li>&#x5982;&#x679C;value&#x662F;&#x4E00;&#x4E2A;&#x6574;&#x6570;&#xFF0C;&#x4F1A;&#x8BDD;&#x5C06;&#x5728;values&#x79D2;&#x6CA1;&#x6709;&#x6D3B;&#x52A8;&#x540E;&#x8FC7;&#x671F;</li>
<li>&#x82E5;&#x679C;value&#x662F;&#x4E00;&#x4E2A;imedelta&#x5BF9;&#x8C61;&#xFF0C;&#x4F1A;&#x8BDD;&#x5C06;&#x5728;&#x5F53;&#x524D;&#x65F6;&#x95F4;&#x52A0;&#x4E0A;&#x8FD9;&#x4E2A;&#x6307;&#x5B9A;&#x7684;&#x65E5;&#x671F;/&#x65F6;&#x95F4;&#x8FC7;&#x671F;</li>
<li>&#x5982;&#x679C;value&#x4E3A;0&#xFF0C;&#x90A3;&#x4E48;&#x7528;&#x6237;&#x4F1A;&#x8BDD;&#x7684;Cookie&#x5C06;&#x5728;&#x7528;&#x6237;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x5173;&#x95ED;&#x65F6;&#x8FC7;&#x671F;</li>
<li>&#x5982;&#x679C;value&#x4E3A;None&#xFF0C;&#x90A3;&#x4E48;&#x4F1A;&#x8BDD;&#x6C38;&#x4E0D;&#x8FC7;&#x671F;</li>
<li>&#x4FEE;&#x6539;&#x89C6;&#x56FE;&#x4E2D;login_handle&#x51FD;&#x6570;&#xFF0C;&#x67E5;&#x770B;&#x6548;&#x679C;</li>
</ul>
<pre><code>def login_handle(request):
    request.session[&apos;uname&apos;] = request.POST[&apos;uname&apos;]
    # request.session.set_expiry(10)
    # request.session.set_expiry(timedelta(days=5))
    # request.session.set_expiry(0)
    # request.session.set_expiry(None)
    return redirect(reverse(&apos;main:index&apos;))
</code></pre><h4 id="&#x5B58;&#x50A8;session">&#x5B58;&#x50A8;session</h4>
<ul>
<li>&#x4F7F;&#x7528;&#x5B58;&#x50A8;&#x4F1A;&#x8BDD;&#x7684;&#x65B9;&#x5F0F;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;settings.py&#x7684;SESSION_ENGINE&#x9879;&#x6307;&#x5B9A;</li>
<li>&#x57FA;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x7684;&#x4F1A;&#x8BDD;&#xFF1A;&#x8FD9;&#x662F;django&#x9ED8;&#x8BA4;&#x7684;&#x4F1A;&#x8BDD;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;&#xFF0C;&#x9700;&#x8981;&#x6DFB;&#x52A0;django.contrib.sessions&#x5230;&#x7684;INSTALLED_APPS&#x8BBE;&#x7F6E;&#x4E2D;&#xFF0C;&#x8FD0;&#x884C;manage.py migrate&#x5728;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x5B89;&#x88C5;&#x4F1A;&#x8BDD;&#x8868;&#xFF0C;&#x53EF;&#x663E;&#x793A;&#x6307;&#x5B9A;&#x4E3A;</li>
</ul>
<pre><code>SESSION_ENGINE=&apos;django.contrib.sessions.backends.db&apos;
</code></pre><ul>
<li>&#x57FA;&#x4E8E;&#x7F13;&#x5B58;&#x7684;&#x4F1A;&#x8BDD;&#xFF1A;&#x53EA;&#x5B58;&#x5728;&#x672C;&#x5730;&#x5185;&#x5728;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x4E22;&#x5931;&#x5219;&#x4E0D;&#x80FD;&#x627E;&#x56DE;&#xFF0C;&#x6BD4;&#x6570;&#x636E;&#x5E93;&#x7684;&#x65B9;&#x5F0F;&#x8BFB;&#x5199;&#x66F4;&#x5FEB;</li>
</ul>
<pre><code>SESSION_ENGINE=&apos;django.contrib.sessions.backends.cache&apos;
</code></pre><ul>
<li>&#x53EF;&#x4EE5;&#x5C06;&#x7F13;&#x5B58;&#x548C;&#x6570;&#x636E;&#x5E93;&#x540C;&#x65F6;&#x4F7F;&#x7528;&#xFF1A;&#x4F18;&#x5148;&#x4ECE;&#x672C;&#x5730;&#x7F13;&#x5B58;&#x4E2D;&#x83B7;&#x53D6;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5219;&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x83B7;&#x53D6;</li>
</ul>
<pre><code>SESSION_ENGINE=&apos;django.contrib.sessions.backends.cached_db&apos;
</code></pre><h4 id="&#x4F7F;&#x7528;redis&#x7F13;&#x5B58;session">&#x4F7F;&#x7528;Redis&#x7F13;&#x5B58;session</h4>
<ul>
<li>&#x4F1A;&#x8BDD;&#x8FD8;&#x652F;&#x6301;&#x6587;&#x4EF6;&#x3001;&#x7EAF;cookie&#x3001;Memcached&#x3001;Redis&#x7B49;&#x65B9;&#x5F0F;&#x5B58;&#x50A8;&#xFF0C;&#x4E0B;&#x9762;&#x6F14;&#x793A;&#x4F7F;&#x7528;redis&#x5B58;&#x50A8;</li>
<li>&#x5B89;&#x88C5;&#x5305;</li>
</ul>
<pre><code>pip install django-redis-sessions
</code></pre><ul>
<li>&#x4FEE;&#x6539;settings&#x4E2D;&#x7684;&#x914D;&#x7F6E;&#xFF0C;&#x589E;&#x52A0;&#x5982;&#x4E0B;&#x9879;</li>
</ul>
<pre><code>SESSION_ENGINE = &apos;redis_sessions.session&apos;
SESSION_REDIS_HOST = &apos;localhost&apos;
SESSION_REDIS_PORT = 6379
SESSION_REDIS_DB = 0
SESSION_REDIS_PASSWORD = &apos;&apos;
SESSION_REDIS_PREFIX = &apos;session&apos;
</code></pre><ul>
<li>&#x7BA1;&#x7406;redis&#x7684;&#x547D;&#x4EE4;</li>
</ul>
<pre><code>&#x542F;&#x52A8;&#xFF1A;sudo redis-server /etc/redis/redis.conf
&#x505C;&#x6B62;&#xFF1A;sudo redis-server stop
&#x91CD;&#x542F;&#xFF1A;sudo redis-server restart
redis-cli&#xFF1A;&#x4F7F;&#x7528;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x670D;&#x52A1;&#x5668;
keys *&#xFF1A;&#x67E5;&#x770B;&#x6240;&#x6709;&#x7684;&#x952E;
get name&#xFF1A;&#x83B7;&#x53D6;&#x6307;&#x5B9A;&#x952E;&#x7684;&#x503C;
del name&#xFF1A;&#x5220;&#x9664;&#x6307;&#x5B9A;&#x540D;&#x79F0;&#x7684;&#x952E;
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../part3/5.html" class="navigation navigation-prev " aria-label="Previous page: Response对象"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../part3/7.html" class="navigation navigation-next " aria-label="Next page: 总结与作业"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
